<template>
    <div class="goodshop">
        <div class="good-item"  v-for="(item) in goodlist" :key="item.id"  @click="getGoodsinfo(item.id)">
            <img :src="item.img_url" >
            <h2 class="good-title">{{item.title}}</h2>
            <div class="info">
                <p class="price" >
                    <span class="now">¥{{item.sell_price}}</span>
                    <span class="old">¥{{item.market_price}}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩余 {{item.stock_quantity}}件</span>
                </p>
            </div>
        </div>

        <!-- 加载更多 -->
        <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>
        
        
    </div>
</template>

<script>
//引入axios文件
import {getGood} from '@/api';
// import
export default {
    data(){
        return{
            pageindex:1,//默认当前页面是1
            goodlist:[],
            id:[],
        }
    },
    created(){
        this.getGoods({pageindex:this.pageindex});;
    },
    methods: {
        //获取页面数据
        getGoods(pageIndex){
            getGood(pageIndex).then(res=>{
                // console.log(res,8877);
                if(res.data.status==0){
                    this.goodlist= this.goodlist.concat(res.data.message)
                    // this.id = res.data.message.id;
                    // console.log(res.data.message.id,111);
                    
                }
                // console.log(this.goodlist);
                // res.data.message.forEach(element => {
                //    console.log(element.id) 
                //    this.id = element.id
                   
                    
                // });
                
                
            });
        },
        getGoodsinfo(id){//这里1的id是形参,需要在结构绑定时传入实际参数item.id
            this.$router.push({name:"GoodsInfo", params: { id } })
        },
        //加载更多
        getMore(){
            this.pageindex++
            this.getGoods({pageindex:this.pageindex})
        }
    },
    
}
</script>

<style lang="less" scoped>
.goodshop{
    display: flex;
    flex-wrap:wrap;
    padding: 8px; 
    justify-content: space-between;//两边对齐
    .good-item{
        width: 49%;
        border: 1px solid #ccc;
        box-shadow: 0 0 8px #ccc;
        margin: 3px 0;
        img{
            width: 100%;
            display: block;
        }
        .good-title{
            font-size: 14px;
        }
        .info{
            background-color: #eee;
            p{
                margin: 0;
                padding: 5px;
            }
            .price{
                .now{
                    color:red;
                    font-weight: 700;
                    font-size: 16px;

                }
                .old{
                    text-decoration: line-through;//贯穿线
                    margin-left: 15px;
                }
            }
            
        }
    }
}
</style>
